<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.cys.exam.interfaces.dao.${package}.${entityName}Dao">

	<!-- 定义数据集 -->
    <resultMap id="${entityName}Map" type="com.cys.exam.${package}.entity.${entityName}">
    <#list dataList as var>
        <#if var_index == 0>
            <id column="${var.columnName}" property="${var.newColumn}"/>
            <result column="${var.columnName}" property="${var.newColumn}"/>
        <#else>
            <result column="${var.columnName}" property="${var.newColumn}"/>
        </#if>
    </#list>
    </resultMap>

    <sql id="selectField">
    <#list dataList as var>	
    	a.${var.columnName}<#if var_has_next>,</#if>
    </#list>
    </sql>

	<!-- 获取数据列表 -->
    <select id="findList" parameterType="map" resultMap="${entityName}Map">
        SELECT 
        	<include refid="selectField" />
        FROM 
        	${tableName} a
    </select>

	<!-- 通过ID加载数据 -->
    <select id="findEntityById" parameterType="Long" resultMap="${entityName}Map">
        SELECT
        	 <include refid="selectField" />
        FROM 
        	${tableName} a 
        WHERE
    <#list dataList as var>
        <#if var_index == 0>
            a.${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
        </#if>
    </#list>
    </select>

    <!-- 分页列表 -->
    <select id="findPage" parameterType="map" resultMap="${entityName}Map">
        SELECT
            <include refid="selectField" />
        FROM
            ${tableName} a
    </select>

    <!-- 通过id进行物理删除 -->
    <delete id="deleteById">
        DELETE FROM
        	${tableName}
	 	WHERE
    <#list dataList as var>
        <#if var_index == 0>
           ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
        </#if>
    </#list>
    </delete>
    
    <!-- 通过ID进行批量物理删除 -->
    <delete id="deleteBatch">
        DELETE FROM
        	${tableName}
	 	WHERE
    <#list dataList as var>
        <#if var_index == 0>
            ${var.columnName} IN
        </#if>
    </#list>
      	<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
           ${r"#{"}item${r"}"}
       </foreach>
    </delete>
    

    <!-- 通过id进行逻辑删除 -->
    <update id="logicDeleteById">
        UPDATE
            ${tableName}
        SET
            DEL_STATUS = 2
        WHERE
    <#list dataList as var>
        <#if var_index == 0>
            ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
        </#if>
    </#list>
    </update>

	<!-- 通过ID进行批量逻辑删除 -->
   <update id="logicDeleteBatch">
        UPDATE 
        	${tableName}
        SET
        	DEL_STATUS = 2
        WHERE
   <#list dataList as var>
       <#if var_index == 0>
           ${var.columnName} IN
       </#if>
   </#list>
       <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
               ${r"#{"}item${r"}"}
       </foreach>
    </update>



	<!-- 根据id更新数据 -->
    <update id="update" parameterType="com.cys.exam.interfaces.entity.${package}.I${entityName}">
        UPDATE 
        	${tableName}
        SET 
	 <#list dataList as var>
         <#if var.newColumn != "createDate" && var.newColumn != "modifyDate">
	        ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
         </#if>
         <#if var.newColumn == "modifyDate">
            ${var.columnName} = now()<#if var_has_next>,</#if>
         </#if>
	 </#list>    	
        WHERE
    <#list dataList as var>
        <#if var_index == 0>
            ${var.columnName} = ${r"#{"}${var.newColumn}${r"}"}
        </#if>
    </#list>
    </update>

	<!-- 插入数据 -->
    <insert id="save" parameterType="com.cys.exam.interfaces.entity.${package}.I${entityName}">
        INSERT INTO 
        	${tableName}(
        	<#list dataList as var>	
		    	${var.columnName}<#if var_has_next>,</#if>
		    </#list>
        	)VALUES(
    <#list dataList as var>
        <#if var.newColumn != "createDate" && var.newColumn != "modifyDate">
                ${r"#{"}${var.newColumn}${r"}"}<#if var_has_next>,</#if>
        </#if>
        <#if var.newColumn == "createDate" || var.newColumn == "modifyDate">
                now()<#if var_has_next>,</#if>
        </#if>
    </#list>
        	)
    </insert>

</mapper>